﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Util;

namespace AutoSigner
{
    public partial class Settings : PhoneApplicationPage
    {
        public Settings()
        {
            InitializeComponent();

            Cancel = (ApplicationBarIconButton) ApplicationBar.Buttons[1];

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            int? id = AppSettings.UserId.Value;
            if ( id.HasValue )
            {
                this.textBox1.Text = id.Value.ToString(CultureInfo.InvariantCulture);
            }

            Cancel.IsEnabled = id != null;
          

        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            
        }

        private bool Validate()
        {
            var id = 0;
            return  !string.IsNullOrEmpty(textBox1.Text) && int.TryParse(textBox1.Text, out id);
           
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            if (!Validate())
            {
                MessageBox.Show("必须输入userId");
                return;
            }

            var id = int.Parse(textBox1.Text);
            AppSettings.UserId.Value = id;

            GoToMainPage();
        }

        private void GoToMainPage()
        {
            NavigationService.GoBack();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            GoToMainPage();
        }
    }
}